package gtfaces.utils;
import java.io.*;
import java.util.*;

public class SemiSecurePassword {

	private static int OFFSET = 8;
	
	public static boolean storePassword(String username, String password) {
		String ePass = CeasarSeive(password, OFFSET);
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter("." + username));
			bw.append(ePass);
			bw.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static String getPassword(String username) {
		try {
			Scanner scan = new Scanner(new File("."+username));
			if (scan.hasNextLine())
				return CeasarSeive(scan.nextLine(), -OFFSET);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String CeasarSeive(String toEncode, int offset) {
		if (offset > 10)
			offset = 10;
		if (offset < -10)
			offset = -10;
		StringBuilder sb = new StringBuilder();
		for (char c : toEncode.toCharArray())
			sb.append((char)(c + offset));
		return sb.toString();
	}
}
